﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLib;

namespace CommonDAL
{
    public abstract class DataEntityFactory<TIdentity, TUniqueKey, TBusinessEntity, TDataEntity, TLazyEntity> :
        IDataEntityFactory<TIdentity, TUniqueKey, TBusinessEntity, TDataEntity, TLazyEntity>
        where TIdentity : struct
        where TUniqueKey : struct
        where TBusinessEntity : class, IBusinessEntity<TUniqueKey>
        where TDataEntity : class, IDataEntity<TIdentity, TUniqueKey, TBusinessEntity>, TBusinessEntity
        where TLazyEntity : LazyDataEntity<TIdentity, TUniqueKey, TBusinessEntity, TDataEntity>, TBusinessEntity
    {
        private UnitOfWork _unitOfWork;

        public DataEntityFactory(UnitOfWork uow)
        {
            _unitOfWork = uow;
        }

        public virtual TDataEntity Create(
            TIdentity identity, TBusinessEntity businessEntity, long version)
        {
            TDataEntity entity = CreateEntity(identity, businessEntity, version);
            _unitOfWork.RegiterClean(entity);
            return entity;
        }


        public virtual TDataEntity Create(TBusinessEntity businessEntity)
        {
            TDataEntity entity = CreateEntity(businessEntity);
            _unitOfWork.RegisterNew(entity);
            return entity;
        }


        public virtual TLazyEntity Create(TIdentity identity)
        {
            return CreateEntity(identity);
        }

        protected abstract TDataEntity CreateEntity(
            TIdentity identity, TBusinessEntity businessEntity, long version);

        protected abstract TDataEntity CreateEntity(TBusinessEntity businessEntity);
        protected abstract TLazyEntity CreateEntity(TIdentity identity);
    }
}
